$(function(){
   const form = layui.form
    // 创建规则
    form.verify({
        pwb:[/^\s{6,12}$/,'密码必须为6-12位的数字或字母'],
        nPwd: function(a){
            const api = $('input[name=oldPwd]').val()
            if(a === api) return '旧密码不能与新密码一样'
        },
        rPwb: function(a){
            const pub = $('input[name=newPwd]').val()
            if(a !== pub) return '两次密码不相同'
        }
    })
    $('.layui-form').on('submit',function(){
        const data = form.val('pwd-form')
        $.post('/my/updatepwd',data).then(res =>  {
            if(res.status == 1){
                return layui.layer.msg(res.message || '修改密码失败！')
            }
            layui.layer.alert('修改密码成功, 即将跳转到登录...', {
                time: 3 * 1000,
                success: function(layero, index){
                    var timeNum = this.time / 1000, setText = function(start){
                        layer.title((start ? timeNum : --timeNum) + ' 秒后关闭', index);
                    };
                    setText(!0);
                    this.timer = setInterval(setText, 1000);
                    if(timeNum <= 0) clearInterval(this.timer);
                },
                end: function(){
                    // 跳转到登录
                    // 1. 跳转到登录页面 
                    parent.location.href = '/login.html'
                    // 2. 删除token
                    localStorage.removeItem('BIG_EVENT_TOKEN')
                }
            })
        })
        return false
    })










})